// pages/bind-house/bind-house.js

const requestToGetBook = require('../../utils/request.js').requestToGetBook
const requestPutBookPhone = require('../../utils/request.js').requestPutBookPhone

const toBookDeatilPage = require('../../utils/wx-navigate.js').toBookDeatilPage
const toIndexPage = require('../../utils/wx-navigate.js').toIndexPage
const toBillPage = require('../../utils/wx-navigate.js').toBillPage

//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bookList:[],
    phone:"",
    no_book:true,
    no_more_book:true,

    btn_disable:false,
    showResult: false,
    result: { isSuccess: true, text: "添加成功", tip: "你已成功添加所有房源，现在可以去给房东交租了", btnText1: '返回账本' }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    requestToGetBook("renter_phone",res=>{
      let bookList = []
      for (let i = 0; i < res.list.length;i++){
        let book = res.list[i]
        book.isSelect = true
        if (!book.renter_id){
          bookList.push(book)
        }
        
      }

      let no_book = res.list.length === 0?true:false
      let no_more_book = this.isNoMoreBook(res.list)

      this.setData({
        bookList: bookList,
        phone: res.phone,
        no_book: no_book,
        no_more_book: no_more_book
      })
      console.log(bookList)
    })

   
  },

  isNoMoreBook(list){
    for (let i= 0; i < list.length;i++){
      let item = list[i]
      if (item.renter_id) {
        console.log(item.renter_id)
      }else{
        return false
      }
    }

    return true
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  bindtap: function (e) {
    
    let bookList = this.data.bookList
    let index = e.currentTarget.dataset.index
    let isSelect = bookList[index].isSelect?true:false
    bookList[index].isSelect = !isSelect
    this.setData({
      bookList: bookList
    })

    
    let btn_disable = false
    for (let i = 0; i < bookList.length; i++) {
      if (bookList[i].isSelect === true) {
        btn_disable = false
        break
      } else {
        btn_disable = true
      }
    }

    this.setData({
      btn_disable: btn_disable
    })
  },

  catchtap: function (e) {
    let bookList = this.data.bookList
    let index = e.currentTarget.dataset.index
    let isOpen = bookList[index].isOpen ? true : false
    bookList[index].isOpen = !isOpen
    this.setData({
      bookList: bookList
    })
  },


  bindbtntap:function(){
    
    let book_ids=[]

    this.data.bookList.forEach(item=>{
      if (item.isSelect){
        book_ids.push(item._id)
      }
    })
    requestPutBookPhone(book_ids,res=>{
      
      app.updateIndexPageData()
      app.updateBillPageData()
      this.setData({
        showResult:true
      })
      wx.setNavigationBarTitle({
        title: "添加成功"
      })

    })
  },

  bindresultbtn1tap: function (e) {
    toIndexPage()
  },
  bindresultbtn2tap: function (e) {
    toIndexPage()
    
  },

  backToIndexPage: function (e) {
    toIndexPage()

  },

})